﻿using LIB.Core;
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Data;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class wht
{

    [OperationContract]
    public string Fs_LOGIN(string b_user, string b_pass)
    {
        try
        {
            c_ht.P_LOGIN(b_user, b_pass);
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" +ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_LOGOUT()
    {
        try
        {
            System.Web.HttpContext.Current.Session.RemoveAll();
            return "";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    [OperationContract]
    public string Fs_NGANHHOC()
    {
        try
        {
            DataTable b_dt = new DataTable();
            b_dt = c_ht.Fdt_NGANH_HOC_LKE();
            string id_nghanh = string.Empty;
            id_nghanh = (b_dt.Rows.Count == 0 ? null : b_dt.Rows[0]["id_nganhdt"].ToString());
            DataTable b_dt_lop = c_ht.Fdt_LOP_HOC_LKE(id_nghanh);
            DataTable b_bac_dt = c_ht.Fdt_DT_LKE();
            return LibBang.Fs_BANG_JSON(b_dt) + "@" + LibBang.Fs_BANG_JSON(b_dt_lop) + "@" + LibBang.Fs_BANG_JSON(b_bac_dt);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    [OperationContract]
    public string Fs_LOPNHANG(string id_nganh, string b_loaidt)
    {
        try
        {
            DataTable b_dt_lop = c_ht.Fdt_LOP_HOC_LKE(id_nganh);       
            return LibBang.Fs_BANG_JSON(b_dt_lop);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
   }
    [OperationContract]
    public string Fs_SINH_VIEN_TIM(string b_ma, string b_ten, string b_lop)
    {
        try
        {
            DataTable b_dt = new DataTable();
            b_dt=c_ht.Fdt_TIM_SINHVIEN(b_ma, b_ten, b_lop);
            if (b_dt.Rows.Count == 0) throw new Exception("loi:Không có số liệu:loi");
            return LibBang.Fs_BANG_JSON(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" +ex.Message +":loi";
        }
    }
    //Lấy toàn bộ thông tin
    [OperationContract]
    public string Fs_TT_TKB()
    {

        try
        {
            DataTable b_dt = new DataTable();
            b_dt = c_ht.Fdt_NGANH_HOC_LKE();
            string id_nghanh = string.Empty;
            id_nghanh = (b_dt.Rows.Count == 0 ? null : b_dt.Rows[0]["id_nganhdt"].ToString());
            DataTable b_dt_lop = c_ht.Fdt_LOP_HOC_LKE(id_nghanh);
            DataTable b_bac_dt = c_ht.Fdt_DT_LKE();
            //Chọn bộ môn
            DataTable b_dt_bm = new DataTable();
            b_dt_bm = c_ht.Fdt_BOMON_LKE();
            //Năm học
            DataTable b_dt_namhoc = new DataTable();
            b_dt_namhoc = c_ht.Fdt_NAM_HOC();
            //Loại
            DataTable b_dt_dt = LIB.Data.SqlData.GetData("PHinhThuc_DT_LKE");

            return LibBang.Fs_BANG_JSON(b_dt) + "@" + LibBang.Fs_BANG_JSON(b_dt_lop) + "@" + LibBang.Fs_BANG_JSON(b_bac_dt)
                    + "@" + LibBang.Fs_BANG_JSON(b_dt_bm) + "@" + LibBang.Fs_BANG_JSON(b_dt_namhoc) + "@" + LibBang.Fs_BANG_JSON(b_dt_dt);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    //Lấy thông tin môn học theo bộ môn
    [OperationContract]
    public string Fs_MM_BOMON(string id_bm)
    {
        try
        {
            DataTable b_dt_mon = new DataTable();
            b_dt_mon = c_ht.Fdt_MON_NGANH(id_bm);
            return LibBang.Fs_BANG_JSON(b_dt_mon);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }

    }
    //Dang ky thou khoa bieu
    [OperationContract]
    public string Fs_DK_TKB(string id_tkb, string b_tg, object[] a_ten, object[] a_gtri, object[] a_hoctrinh)
    {
        try
        {
            DataTable b_dt = LibBang.Fdt_TAO_BANG(LibConvert.Fa_OBJ_STR(a_ten), LibConvert.Fa_OBJ_STR(a_gtri));
            if (b_dt.Rows.Count == 0) throw new Exception("loi:Không lấy được số liệu:loi");
            DataRow b_dr = b_dt.Rows[0];
            DataTable b_dt_tso = new DataTable();
            LibBang.P_THEM_COT(ref b_dt_tso, new string[] { "ten", "hs" });

            object[] id = c_ht.P_TKB_THEM(id_tkb, b_tg, LibConvert.OBJ_STR(b_dr["lop"]), LibConvert.OBJ_STR(b_dr["mon_hoc"]),
                LibConvert.OBJ_STR(b_dr["giang_vien"]), LibConvert.OBJ_STR(b_dr["loai_dt"]), LibConvert.OBJ_STR(b_dr["nam_hoc"]),
                 LibConvert.ToInt(b_dr["tin_chi"]), LibConvert.toString(b_dr["thoi_gian"]), LibConvert.toString(b_dr["tiet_hoc"])
                 , LibConvert.toString(b_dr["hoc_ky"]), LibConvert.toString(b_dr["giang_duong"]), LibConvert.ToFloat(b_dr["hso_tinh"]), LibConvert.toString(b_dr["loai_mon"]));

            DataTable b_dt_tinh = new DataTable();
            LibBang.P_THEM_COT(ref b_dt_tinh, new string[] { "id_nkb", "ten", "hs" });
            for (int i = 0; i < a_hoctrinh.Length; i++)
            {
                string[] a_loai = new string[b_dt_tinh.Columns.Count];
                a_loai[0] = id[0].ToString();
                a_loai[1] = ((object[])a_hoctrinh[i])[0].ToString();
                a_loai[2] = ((object[])a_hoctrinh[i])[1].ToString();
                LibBang.P_THEM_HANG(ref b_dt_tinh, a_loai);
            }
            c_ht.P_TINH_DIEM(b_dt_tinh);
            DataTable b_dt_tim = c_ht.Fdt_TKB_ALL(LibConvert.OBJ_STR(b_dr["nam_hoc"]), LibConvert.OBJ_STR(b_dr["lop"]), LibConvert.toString(b_dr["hoc_ky"]));
            return id.ToString() + "@" + LibBang.Fs_BANG_JSON(b_dt_tim);
        }
        catch (Exception ex)
        {
            return "loi:loi" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_TKB_TIM(string b_nam, string b_lop, string hk)
    {
        try
        {
            DataTable b_dt = c_ht.Fdt_TKB_ALL(b_nam, b_lop, hk);
            return LibBang.Fs_BANG_JSON(b_dt);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    [OperationContract]
    public string Fs_TKB_CT(string id, string id_lop, string id_mon, string tgian, string hk)
    {
        try
        {
            DataSet b_ds = new DataSet();
            DataTable b_dt = new DataTable();
            b_ds = c_ht.Fdt_TKB_CT(id, id_lop, id_mon, tgian, hk);
            b_dt = b_ds.Tables[0];
            if (b_dt.Rows.Count == 0) return "";
            string id_bmn = b_dt.Rows[0]["bo_mon"].ToString();
            DataTable b_dt_mon = c_ht.Fdt_MON_NGANH(id_bmn);
            //Chuyen lop
            DataTable b_dt_lop = c_ht.Fdt_LOP_HOC_LKE(b_dt.Rows[0]["chuyen_nganh"].ToString());

            return LibBang.Fs_BANG_JSON(b_dt_mon) + "@" + LibBang.Fs_BANG_JSON(b_dt) + "@" + LibBang.Fs_BANG_JSON(b_dt_lop)
                             + "@" + LibBang.Fs_BANG_JSON(b_ds.Tables[1]);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_TKB_XOA(string b_namhoc, string id, string id_lop, string id_mon, string tgian, string hk)
    {
        try
        {
            c_ht.P_TKB_XOA(id, id_lop, id_mon, tgian, hk);
            DataTable b_dt = c_ht.Fdt_TKB_ALL(b_namhoc, id_lop, hk);
            return LibBang.Fs_BANG_JSON(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    
    // In thời khóa biểu
    [OperationContract]
    public string Fs_TKB_IN(string id_nam, string lopid, string hkid)
    {
        try
        {
            DataTable b_dt = new DataTable(), b_dt_ct=new DataTable();
            b_dt = LIB.Data.SqlData.GetData(new string[] { id_nam, lopid, hkid}, "PTKB_IN");
            if(b_dt.Rows.Count==0) throw new Exception("loi:Chưa đăng ký thời khóa biểu:loi");
            //Khơi tạo table
            DataTable b_dt_kb = new DataTable("B");
            LibTable.AddNewCol(ref b_dt_kb, new string[] { "STT", "T2", "T3", "T4", "T5", "T6", "T7", "CN" });

            //Duyệt mảng DataRow
            DataRow[] b_row_t2 = b_dt.Select("T='T2'");
            DataRow[] b_row_t3 = b_dt.Select("T='T3'");
            DataRow[] b_row_t4 = b_dt.Select("T='T4'");
            DataRow[] b_row_t5 = b_dt.Select("T='T5'");
            DataRow[] b_row_t6 = b_dt.Select("T='T6'");
            DataRow[] b_row_t7 = b_dt.Select("T='T7'");
            DataRow[] b_row_cn = b_dt.Select("T='CN'");

            //Duyệt mảng
            bool b_ok = false;
            int stt = 1, b_dem=0;
            while (b_ok)
            {
                //Khoi tao row mới
                DataRow b_row = b_dt_kb.NewRow();
                b_row["STT"] = stt.ToString();
                if (stt > b_row_t2.Length)
                {
                    b_dem++;
                    b_row["T2"] = "";
                }
                else
                    b_row["T2"] = LibConvert.toString(b_row_t2[stt-1]["MONHOC"]) + " (   " + LibConvert.toString(b_row_t2[stt-1]["tgian"]) + " )"; 
            }
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
}
